<!--

    Copyright © 2016-2025 The Thingsboard Authors

    Licensed under the Apache License, Version 2.0 (the "License");
    you may not use this file except in compliance with the License.
    You may obtain a copy of the License at

        http://www.apache.org/licenses/LICENSE-2.0

    Unless required by applicable law or agreed to in writing, software
    distributed under the License is distributed on an "AS IS" BASIS,
    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    See the License for the specific language governing permissions and
    limitations under the License.

-->
<form [formGroup]="filterFormGroup" (ngSubmit)="save()" style="width: 1200px;">
  <mat-toolbar class="justify-between" color="primary">
    <h2>{{(data.isAdd ? 'alarm-rule.add-filter' : 'alarm-rule.edit-filter') | translate}}</h2>
    <button mat-icon-button
            (click)="cancel()"
            type="button">
      <mat-icon class="material-icons">close</mat-icon>
    </button>
  </mat-toolbar>
  <div mat-dialog-content>
    <section class="tb-form-panel no-border no-padding">
      <section class="tb-form-panel">
        <div class="tb-form-panel-title">{{ 'alarm-rule.general' | translate }}</div>
        <div class="flex flex-row gap-2 xs:flex-col">
          <mat-form-field class="flex-1" appearance="outline" subscriptSizing="dynamic">
            <mat-label translate>alarm-rule.value-argument</mat-label>
            <mat-select formControlName="argument" placeholder="{{ 'action.set' | translate }}">
              @for (argument of argumentsList; track argument) {
                <mat-option [value]="argument" [disabled]="argumentInUse(argument)" >{{ argument }}</mat-option>
              }
            </mat-select>
            @if (filterFormGroup.get('argument').touched && filterFormGroup.get('argument').hasError('required')) {
              <mat-icon matSuffix
                        matTooltipPosition="above"
                        matTooltipClass="tb-error-tooltip"
                        [matTooltip]="'calculated-fields.hint.argument-name-required' | translate"
                        class="tb-error !block">
                warning
              </mat-icon>
            }
          </mat-form-field>
          <mat-form-field class="flex-1 tb-value-type" appearance="outline" subscriptSizing="dynamic" hideRequiredMarker>
            <mat-label translate>filter.value-type.value-type</mat-label>
            <mat-select formControlName="valueType">
              <mat-select-trigger>
                <mat-icon class="tb-mat-18" svgIcon="{{ entityKeyValueTypes.get(filterFormGroup.get('valueType').value)?.icon }}"></mat-icon>
                <span>{{ entityKeyValueTypes.get(filterFormGroup.get('valueType').value)?.name | translate }}</span>
              </mat-select-trigger>
              <mat-option *ngFor="let valueType of entityKeyValueTypesKeys" [value]="valueType">
                <mat-icon class="tb-mat-18" svgIcon="{{ entityKeyValueTypes.get(entityKeyValueTypeEnum[valueType]).icon }}"></mat-icon>
                <span>{{ entityKeyValueTypes.get(entityKeyValueTypeEnum[valueType]).name | translate }}</span>
              </mat-option>
            </mat-select>
            <mat-error *ngIf="filterFormGroup.get('valueType').hasError('required')">
              {{ 'filter.value-type-required' | translate }}
            </mat-error>
          </mat-form-field>
        </div>
      </section>

      <section class="tb-form-panel">
        <div class="flex flex-row items-center justify-between">
          <div class="tb-form-panel-title"
               tb-hint-tooltip-icon="{{ filterFormGroup.get('valueType').value === entityKeyValueTypeEnum.DATE_TIME ? ('alarm-rule.date-time-hint' | translate) : '' }}">
            {{ 'alarm-rule.filters' | translate }}
          </div>
          <tb-toggle-select formControlName="operation"
                            selectMediaBreakpoint="xs">
            <tb-toggle-option [value]="ComplexOperation.AND">{{ complexOperationTranslationMap.get(ComplexOperation.AND) | translate }}</tb-toggle-option>
            <tb-toggle-option [value]="ComplexOperation.OR">{{ complexOperationTranslationMap.get(ComplexOperation.OR) | translate }}</tb-toggle-option>
          </tb-toggle-select>
        </div>
        <tb-alarm-rule-filter-predicate-list [valueType]="filterFormGroup.get('valueType').value"
                                             [operation]="filterFormGroup.get('operation').value"
                                             [arguments]="arguments"
                                             [argumentInUse]="filterFormGroup.get('argument').value"
                                             formControlName="predicates">
        </tb-alarm-rule-filter-predicate-list>
      </section>
    </section>
  </div>
  <div mat-dialog-actions class="flex items-center justify-end">
    <button mat-button color="primary"
            type="button"
            (click)="cancel()"
            cdkFocusInitial>
      {{ 'action.cancel' | translate }}
    </button>
    <button mat-raised-button color="primary"
            type="submit"
            [disabled]="filterFormGroup.invalid || !filterFormGroup.dirty || !predicatesValid">
      {{ (data.isAdd ? 'action.add' : 'action.update') | translate }}
    </button>
  </div>
</form>
